#include<iostream>
#include<cstdio>
#include<cstring>
#include<unistd.h>
#include<string>
#include<pthread.h>

std::string changeId(const pthread_t& thread_id)
{
    char tid[128];
    snprintf(tid,sizeof(tid),"0x%x",thread_id);
    return tid;
}

void* start_routine(void* args)
{
    std::string threadname = static_cast<const char*>(args);
    int cnt=5;
    //pthread_detach(pthread_self());//设置自己为分离状态
   
    while(cnt--)
    {
        std::cout<< threadname <<" running... : "<<changeId(pthread_self())<<std::endl;
        sleep(1);
    }
    return nullptr;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid,nullptr,start_routine,(void *)"thread 1");
    
    std::string main_id=changeId(pthread_self());
    pthread_detach(tid);
    //std::cout<<" main thread running...new thread id : "<<changeId(tid)<<" main thread id :"<<main_id<<std::endl;

    // //一个线程默认是joinable的，如果设置了分离状态，不能够进行等待了！
    // int n = pthread_join(tid,nullptr);
    // std::cout<<"result: "<<n<<" : "<<strerror(n)<<std::endl;
    while(true)
    {
         std::cout<<" main thread running...new thread id : "<<changeId(tid)<<" main thread id :"<<main_id<<std::endl;
        sleep(1);
    }

    return 0;

}